package com.sqx.modules.errand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.errand.dao.ErrandEvaluateDao;
import com.sqx.modules.errand.dao.TbIndentDao;
import com.sqx.modules.errand.entity.ErrandEvaluate;
import com.sqx.modules.errand.entity.TbIndent;
import com.sqx.modules.errand.service.ErrandEvaluateService;
import com.sqx.modules.order.entity.Evaluate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ErrandEvaluateServiceImpl extends ServiceImpl<ErrandEvaluateDao, ErrandEvaluate> implements ErrandEvaluateService {

    @Autowired
    private ErrandEvaluateDao errandEvaluateDao;
    @Autowired
    private TbIndentDao tbIndentDao;

    @Override
    public Result userEvaluate(Long userId, ErrandEvaluate evaluate) {
        TbIndent indent = tbIndentDao.selectIndentByIndentNumber(evaluate.getIndentNumber());
        evaluate.setUserId(userId);
        evaluate.setRiderUserId(indent.getRiderUserId());
        evaluate.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        errandEvaluateDao.insert(evaluate);
        return Result.success();
    }

    @Override
    public IPage<ErrandEvaluate> getMyEvaluateList(Long userId, Integer page, Integer limit) {
        Page<ErrandEvaluate> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
        }
        return errandEvaluateDao.selectPage(pages, new QueryWrapper<ErrandEvaluate>().eq("user_id", userId));
    }

}
